package controller;

import bean.*;


import framework.GetMapping;
import framework.ModelAndView;
import framework.PostMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.http.HttpRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class IndexController {


    @GetMapping("/")
    public ModelAndView index(HttpSession session) {
        Account account = (Account) session.getAttribute("account");
        Random random = new Random();
        int i = random.nextInt(10);
        int j = random.nextInt(10);
        int k = random.nextInt(10);
        while (i == j) {
            j = random.nextInt(10);
        }
        while (k == j || k == i) {
            k = random.nextInt(10);
        }
        if (account == null) {
            account = new Account();
        }
        QuestionDAO daoQ = new QuestionDAO();
        return new ModelAndView("/index.html", Map.of( "account", account,"q1", daoQ.get(i + 1), "q2", daoQ.get(j + 1), "q3", daoQ.get(k + 1)));
    }
//

    @GetMapping("/afterPostQuestion")
    public ModelAndView afterPostQuestion(String id) {
//        Account account = (Account) session.getAttribute("account");
        int ID;
        if (!id.equals("")) {
            ID = Integer.parseInt(id);
        } else {
            ID = 1;
        }
        QuestionDAO daoQ = new QuestionDAO();
        AnswerDAO answerDAO = new AnswerDAO();
        Question question = daoQ.get(ID);
        ArrayList<String> answerList = question.getAnswerList();

        Answer answer = null, answer1 = null, answer2 = null;

        if (answerList == null) {
            return new ModelAndView("/afterPostQuestion.html", Map.of("q", question));
        } else if (answerList.size() == 1) {
            answer = answerDAO.get(Integer.parseInt(answerList.get(0)));
            return new ModelAndView("/afterPostQuestion.html", Map.of("q", question, "a1", answer));
        } else if (answerList.size() == 2) {
            answer = answerDAO.get(Integer.parseInt(answerList.get(0)));
            answer1 = answerDAO.get(Integer.parseInt(answerList.get(1)));
            return new ModelAndView("/afterPostQuestion.html", Map.of("q", question, "a1", answer, "a2", answer1));
        } else if (answerList.size() >= 3) {
            answer = answerDAO.get(Integer.parseInt(answerList.get(0)));
            answer1 = answerDAO.get(Integer.parseInt(answerList.get(1)));
            answer2 = answerDAO.get(Integer.parseInt(answerList.get(2)));
        }

        return new ModelAndView("/afterPostQuestion.html", Map.of("q", question, "a1", answer, "a2", answer1, "a3", answer2));
    }

//    @GetMapping("/afterPostAnswer")
//    public ModelAndView afterPostAnswer(AnswerBean aBean, QuestionBean qBean, HttpSession session) {
//        QuestionDAO daoQ = new QuestionDAO();
//        Question question = new Question();
//        question.setSponsor_id(qBean.sponsor_id);
//        question.setTitle(qBean.title);
//        question.setQuestionDetails(qBean.questionDetails);
//        question.setQuestionID(daoQ.add(question));
//        Answer answer = new Answer();
//        AnswerDAO daoA = new AnswerDAO();
//        answer.setContent(aBean.content);
//        answer.setAnswerID(daoA.add(answer));
//
//        return new ModelAndView("/afterPostQuestion.html", question.getTitle(), answer.getContent());
//    }

    @GetMapping("/register")
    public ModelAndView register() {
        return new ModelAndView("/register.html");
    }

    @PostMapping("/doregister")
    public ModelAndView doregister(SignInBean bean, HttpServletResponse response, HttpSession session) throws IOException {
        AccountDAO dao = new AccountDAO();
        if (dao.get(bean.accountName) != null) {
            response.setContentType("application/json");
            PrintWriter pw = null;
            try {
                pw = response.getWriter();
            } catch (IOException e) {
                e.printStackTrace();
            }
            pw.write("{\"error\":\"Account Name Already Exist\"}");
            pw.flush();
        } else {
            Account account = new Account();
            account.setAccountName(bean.accountName);
            account.setPassword(bean.password);
            session.setAttribute("account", account);
            dao.add(account);
            response.setContentType("application/json");
            PrintWriter pw = null;
            try {
                pw = response.getWriter();
            } catch (IOException e) {
                e.printStackTrace();
            }
            pw.write("{\"result\":true}");
            pw.flush();
        }
        return null;
    }

    @PostMapping("/postQuestion")
    public ModelAndView doPostQuestion(QuestionBean bean, HttpServletResponse response, HttpSession session) throws IOException {
        Account account = (Account) session.getAttribute("account");
        if (account == null) {
            response.setContentType("application/json");
            PrintWriter pw = response.getWriter();
            //pw.write("{\"error\":please signin!}");
            pw.write("{\"error\":\"please signin!\"}");
            pw.flush();
            return null;
        }
        Question question = new Question();
        QuestionDAO daoQ = new QuestionDAO();
        question.setSponsor_id(bean.sponsor_id);
        question.setTitle(bean.title);
        question.setQuestionDetails(bean.questionDetails);
        question.setQuestionID(daoQ.add(question));
        AccountDAO dao = new AccountDAO();
        ArrayList<String> list = account.getQuestionList();
        if (list == null) {
            list = new ArrayList<String>();
            list.add(String.valueOf(question.getQuestionID()));
        } else {
            list.add(String.valueOf(question.getQuestionID()));
        }
        account.setQuestionList(list);
        dao.update(account);

        response.setContentType("application/json");
        PrintWriter pw = response.getWriter();
        pw.write("{\"result\":true}");
        pw.flush();

        return null;
    }


    @PostMapping("/postAnswer")
    public ModelAndView doPostAnswer(AnswerBean bean, HttpServletResponse response, HttpSession session) throws IOException {
        Account account = (Account) session.getAttribute("account");
        Answer answer = new Answer();
        AnswerDAO daoA = new AnswerDAO();
        answer.setContent(bean.content);
        answer.setresponder_id(bean.responder_id);
        answer.setAnswerID(daoA.add(answer));
        AccountDAO dao = new AccountDAO();
        ArrayList<String> list = account.getAnswerList();
        if (list == null) {
            list = new ArrayList<String>();
            list.add(String.valueOf(answer.getAnswerID()));
        } else {
            list.add(String.valueOf(answer.getAnswerID()));
        }
        account.setAnswerList(list);
        dao.update(account);

        response.setContentType("application/json");
        PrintWriter pw = response.getWriter();
        pw.write("{\"result\":true}");
        pw.flush();

        return null;
    }

}
